package com.book.biz.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.book.bean.OrderInfo;
import com.book.bean.OrderListInfo;
import com.book.biz.OrderInfoBiz;
import com.book.dao.OrderInfoDao;
import com.book.dao.OrderInfoListDao;
import com.book.dao.impl.OrderInfoDaoImpl;
import com.book.dao.impl.OrderInfoListDaoImpl;
import com.book.util.DBUtil;


public class OrderInfoBizImpl implements OrderInfoBiz {
	OrderInfoDao orDao = null;
	OrderInfoListDao orIfDao = null;
	public OrderInfoBizImpl() {
		orDao = new OrderInfoDaoImpl();
		orIfDao = new OrderInfoListDaoImpl();
		
	}
	@Override
	public boolean addOrders(OrderInfo orderInfo) {
		Connection conn = null;
		try {
			//设置连接对象 非自动提交
			conn = DBUtil.getConn();
			conn.setAutoCommit(false);
			//生成订单
			orDao.addOrders(orderInfo);
			//获取最新的订单编号
			int orderInd = orDao.getMazId();
			//循环添加订单明细
			for(OrderListInfo orIf:orderInfo.getOrderInfoList()){
				//给订单明细添加订单编号
				orIf.getOrderInfo().setOrder_id(orderInd);
				//添加到明细表中
				orIfDao.addOrderInfo(orIf);
			}
			//提交事务
			conn.commit();
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			//如果有错，回滚事务
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}finally{
			try {
				DBUtil.closeConn();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;
	}
	@Override
	public List<OrderInfo> getOrderByUser(int uid) {
		try {
			return orDao.getOrderByUser(uid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				DBUtil.closeConn();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
		
	}

}
